<?php


namespace App\Log;


use Auth,Saas;

class DomesticBillLog extends AdminLog
{
    protected $pre = [];    //修改前
    protected $next = [];   //修改后
    protected $module;      //操作模块

    protected $mean = [
        'commission' => '佣金金额',
        'zero_money' => '抹零金额',
        'notice_days' => '提醒时间',
        'forbid_days' => '关闭时间',
        'real_money' => '实际金额',
        'pay_status' => [0=>'待支付',1=>'已支付','2'=>'已作废'],
        'commission_rate' => '佣金比例',
        'send_money' => '配送金额',
        'credentials' => '凭证',
        'remark' => '备注',
        'payer' => '付款人',
        'collection_channels' => [0=>'未支付',1=>'银联支付',2=>'银行转账',3=>'支付宝',4=>'微信',5=>'现金'],
        'pay_type' => [0=>'未支付',1=>'在线支付',2=>'线下收款'],
    ];

    protected $other = [    //数组对应字段
        'pay_status' => '状态',
        'collection_channels' => '收款渠道',
        'pay_type' => '支付方式',
    ];

    public function __construct($pre,$next,$module)
    {
        $this->pre = $pre;
        $this->next = $next;
        $this->module = $module;
    }

    public function addLog()
    {
        $this->data = $this->combinedData();
        parent::addLog();
    }


    public function combinedData():array
    {
        $content = [];
        if (!empty( $this->next)){
            foreach ($this->next as $key => $val){
                if (isset( $this->mean[$key])){
                    if (isset( $this->other[$key])){
                        $meanInfo = "'". $this->other[$key]."'";
                    }else{
                        $meanInfo = "'". $this->mean[$key]."'";
                    }
                    if (is_array($this->mean[$key])){
                        $content[$meanInfo] =  $this->mean[$key][$this->pre[$key]].' -> '. $this->mean[$key][$val];
                    }else{
                        if($key=='notice_days' || $key=='forbid_days' )
                        {
                            $content[$meanInfo] =  date('Y-m-d',$this->pre[$key]).' -> '.date('Y-m-d',$this->next[$key]);
                        }else
                        {
                            $content[$meanInfo] =  $this->pre[$key].' -> '. $this->next[$key];
                        }

                    }
                }
            }
        }
        $data = [
            'module' =>  $this->module->pid,
            'operate' =>  $this->module->id,
            'type' =>  $this->module->type,
            'main' =>  isset($this->next['order_sn']) ? $this->next['order_sn'] : $this->pre['order_sn'],
            'main_id' => isset($this->pre['id']) ? $this->pre['id'] : 0,
            'content' => json_encode($content,JSON_UNESCAPED_UNICODE),
            'user_id' => auth('admin')->user()->id,
            'ip'    => get_proxy_ip(),
            'model' => $this->module->model,
        ];

        return $data;
    }
}
